<?php
class Admin_VctSettingController extends Apps_Controller_Action_Admin {
	public function indexAction() {
		$rq   = $this->_request;

    	$tbVctSetting = new M_DbTable_VctSetting();
        $vctSetting   = $tbVctSetting->getVctSetting()->query()->fetch();

        $form = new Admin_Form_AddVctSetting();
        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
                $data = $form->getValues();
                $data['password_email'] = rand(10000, 99999).'vct'.$form->getValue('password_email').'@760211'; //encode
                if(!$vctSetting) {
                    $r = $tbVctSetting->safelyInsert($data);
                } else {
                    $r = $tbVctSetting->safelyUpdate($data, array('id =?' => $vctSetting['id']));
                }
                $this->_flashMessenger->addMessage(
                	$r
                    ? self::$msgAddSuccess
                    : self::$msgAddFailed);
                return $this->goToActionIndex();
            }
        } else {
            if($vctSetting) {
                $passwordEmail = substr(substr($vctSetting['password_email'], 8), 0, -7);
                $vctSetting['password_email'] = $passwordEmail;

                $form->populate($vctSetting);
            }
        }
        $this->view->form = $form;
	}
	
	
	//VCT REPLY MESSAGE TO CUSTOMER 
	public function replyMessageAction() {
		$rq   = $this->_request;

    	$tbVctSetting = new M_DbTable_VctSetting();
        $vctSetting   = $tbVctSetting->getVctSetting()->query()->fetch();

        $form = new Admin_Form_AddVctReplyMessage();
        if($rq->isPost()) {
            if($form->isValid($rq->getParams())) {
                $data = $form->getValues();
                if(!$vctSetting) {
                    $r = $tbVctSetting->safelyInsert($data);
                } else {
                    $r = $tbVctSetting->safelyUpdate($data, array('id =?' => $vctSetting['id']));
                }
                $this->_flashMessenger->addMessage(
                	$r
                    ? self::$msgUpdateSuccess
                    : self::$msgUpdateFailed);
                return $this->_redirect('/admin/vct-setting/reply-message');
            }
        } else {
            if($vctSetting) {
                $form->populate($vctSetting);
            }
        }
        $this->view->form = $form;
	}
}
